<template>
<view>
<form @reset="formReset" @submit="release" reportSubmit="true">
	<view class="modle" style="border-bottom:0">
		配件详图
	</view>
	<!-- <block wx:if="{{tyid==1}}"> -->
		<view v-for="(item, index) in carsimgs" :key="index" class="shangchuan_view" v-if="item.img!=''">
			<image @tap="upthiscarsimgs" class="shangchuan_Img_close" :data-index="index" src="/static/pages/image/tupiangb_btn.png"></image>
			<image @tap="showimgs" class="shangchuan_Img" :data-index="index" :src="item.img"></image>
		</view>
		<view class="shangchuan_view" v-if="carsimgs.length<=9">
			<image @tap="upcarsimgs" class="shangchuan_Img" src="/static/pages/image/tianjiantp_btn.png"></image>
		</view>
	<!-- </block> -->
	<!-- <block wx:if="{{tyid==2}}">
		<view class="shangchuan_view" wx:if="{{item!=''}}" wx:for="{{carsimgs}}" wx:key="key">
			<image bindtap="upthiscarsimgs" class="shangchuan_Img_close" data-index="{{index}}" src="/pages/image/tupiangb_btn.png"></image>
			<image bindtap="showimgs" class="shangchuan_Img" data-index="{{index}}" src="{{item.img}}"></image>
		</view>
		<view class="shangchuan_view" wx:if="{{carsimgs.length<=9}}">
			<image bindtap="upcarsimgs" class="shangchuan_Img" src="/pages/image/tianjiantp_btn.png"></image>
		</view>
	</block> -->
	<view class="modle">
		店铺名称
		<text class="must_red">*</text>
		<input class="modle_right" name="title" placeholder="请输入店铺名称" placeholderStyle="color: #9295A8;" type="text" :value="title">
	</view>
	<view class="modle">
		<input class="modle_right btm_input" name="tel" placeholder="授权获取电话" placeholderStyle="color: #9295A8;" type="number" :value="tel">
		联系电话
		<text class="must_red">*</text>
		<button @getphonenumber="getPhoneNumber" class="modle_btn" open-type="getPhoneNumber">电话授权</button>
	</view>
  <view class="modle">
		地址
		<text class="must_red">*</text>
		<!-- <input class="modle_right" name="addr" placeholder="请输入地址" placeholderStyle="color: #9295A8;" type="text" value="{{addr}}"></input> -->
		<view class="modle_right">
			<region-picker @change="bindRegionChange" class="serch_border_area_border" :customItem="customItem" :value="region">
				<view class="picker">
					{{region[0]||(addr!=null&&addr!='')?addr:'请选择'}}
				</view>
			</region-picker>
		</view>
	</view>
	<view class="modle line_bott">
		内容
		<text class="must_red">*</text>
		<!-- <input class="modle_right" name="carname" placeholder="请输入内容" placeholderStyle="color: #9295A8;" type="text" value="{{defult_value}}"></input> -->

	</view>
	<view class="texta_box">
		<textarea placeholder="请输入内容" name="content" @input="inpCont" maxlength="-1" :value="content"></textarea>
	</view>
	<block v-if="tyid==1">
		<button class="bufa_btn" style="background: #333;" v-if="status=='1'">已经禁止{{st_word}}信息</button>
		<button class="bufa_btn" form-type="submit" v-else>{{st_word}}</button>
	</block>
	<block v-if="tyid==2">
		<button class="bufa_btn" style="background: #333;" v-if="status=='1'">已经禁止{{st_word}}信息</button>
		<button class="bufa_btn" form-type="submit" v-else>修改</button>
	</block>
	<view class="jinggao">{{st_word}}不真实信息，虚报价格，一经核查将做封号处理。</view>
</form>
<!--<import src="/pages/alert/index.wxml"></import>-->
<block data-type="template" data-is="alert" data-attr="isShow:isShow">
    <view class="wx_dialog_container" :hidden="!isShow">
        <view class="wx-alert-mask"></view>
        <view class="wx-alert-dialog">
            <view class="wx-alert-dialog-title">尊敬的用户</view>
            <view class="wx-alert-dialog-content">尚未获取到您的用户信息，较多功能无法体验，请允许获取用户信息</view>
            <view class="wx-alert-dialog-footer">
                <button @tap="hideDialog" class="wx-alert-dialog-btn">拒绝</button>
                <button @getuserinfo="updateUserInfo" class="wx-alert-dialog-btn" open-type="getUserInfo">允许</button>
            </view>
        </view>
    </view>
</block>
</view>
</template>

<script>
var app = getApp();

export default {
  data() {
    return {
      carsimgs: [{
        img: "",
        imgshort: ""
      }],
      title: '',
      tel: '',
      content: '',
      isShow: !1,
      buttscan: !1,
      tyid: '',
      id: '',
      addr: '',
      region: ["", "", ""],
      st_word: '',
      customItem: "",
      status: ""
    };
  },

  components: {},
  props: {},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.check_st();
    that.setData({
      tyid: options.tid,
      id: options.id
    });

    if (options.tid == 2) {
      that.getPartInfo(options.id);
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  methods: {
    check_st: function () {
      var that = this;
      app.globalData.util.request({
        url: "entry/wxapp/Api",
        data: {
          r: "home.index.change_word",
          m: "monai_market"
        },
        cachetime: "0",
        success: function (res) {
          console.log('abc:', res.data);
          var json = res.data.data;
          wx.setNavigationBarTitle({
            title: res.data.data
          });
          that.setData({
            st_word: res.data.data
          });
        }
      });
    },
    // 切换地区
    bindRegionChange: function (a) {
      var that = this;
      var t = "不限" == a.detail.value[0] ? "" : a.detail.value[0],
          e = "不限" == a.detail.value[1] ? "" : a.detail.value[1],
          i = "不限" == a.detail.value[2] ? "" : a.detail.value[2],
          addr_str = t + '-' + e + '-' + i;
      that.setData({
        region: [t, e, i],
        addr: addr_str
      });
      wx.setStorageSync('addr', that.region);
    },
    //获取配件信息
    getPartInfo: function (e) {
      var that = this;
      app.globalData.util.request({
        url: "entry/wxapp/Api",
        data: {
          m: "monai_market",
          r: "home.index.user_sbkdetial",
          id: e,
          uid: app.globalData.getuid()
        },
        cachetime: "0",
        success: function (res) {
          console.log('配件信息：', res.data);
          var json = res.data.data;
          that.setData({
            carsimgs: json.img_str,
            title: json.title,
            tel: json.tel,
            content: json.content,
            addr: json.address
          });
        }
      });
    },
    //获取电话号码
    getPhoneNumber: function (a) {
      console.log(a);
      var t = this;
      app.globalData.util.request({
        url: "entry/wxapp/Api",
        data: {
          r: "sale.index.userphone",
          m: "monai_market",
          iv: a.detail.iv,
          encryptedData: a.detail.encryptedData
        },
        cachetime: "0",
        success: function (a) {
          t.setData({
            tel: a.data.data
          });
        }
      });
    },
    //点击发布按钮
    release: function (e) {
      var that = this,
          uid = app.globalData.getuid();

      if (uid) {
        that.setData({
          buttscan: !0
        });
        var name = e.detail.value.title;
        if ("" == name || "undefined" == name) return app.globalData.util.message({
          title: "请输入店铺名称",
          type: "error"
        }), void that.setData({
          buttscan: !1
        });
        var addr = that.addr;
        if ("" == addr || "undefined" == addr) return app.globalData.util.message({
          title: "请选择地址",
          type: "error"
        }), void that.setData({
          buttscan: !1
        });
        var tel = e.detail.value.tel;
        if ("" == tel || "undefined" == tel) return app.globalData.util.message({
          title: "请输入电话号码",
          type: "error"
        }), void that.setData({
          buttscan: !1
        });
        var cont = e.detail.value.content;
        if ("" == cont || "undefined" == cont) return app.globalData.util.message({
          title: "请输入内容",
          type: "error"
        }), void that.setData({
          buttscan: !1
        });
        var p = that.carsimgs;
        console.log('p:', p[0]);
        if (p.length < 3) return app.globalData.util.message({
          title: "请至少添加3张配件图片",
          type: "error"
        }), void that.setData({
          buttscan: !1
        });
        var g = [];

        for (var h in p) "" != p[h].imgshort && "" != p[h].img && (console.log(p[h]), console.log(p[h].imgshort), g.push(p[h]));

        console.log('g:', g);

        if (that.tyid == 1) {
          //发布
          app.globalData.util.request({
            url: "entry/wxapp/Api",
            data: {
              m: "monai_market",
              r: "home.index.fbsbk",
              uid: uid,
              // 用户id
              title: name,
              // 店铺名
              tel: tel,
              // 联系电话
              content: cont,
              // 内容
              carimgs: g,
              // 图片（参考卖车页面的）
              address: addr
            },
            cachetime: "0",
            success: function (res) {
              console.log('发布成功：', res.data);
              wx.showModal({
                content: "提交成功",
                cancelText: "继续添加",
                confirmText: "好的",
                success: function (a) {
                  a.confirm ? wx.switchTab({
                    url: '../index/index'
                  }) : a.cancel && (that.setData({
                    buttscan: !1
                  }), wx.reLaunch({
                    url: "/pagesA/parts/parts"
                  }));
                }
              });
            },
            fail: function (res) {
              console.log('发布失败：', res.data);
            }
          });
        } else {
          //修改
          app.globalData.util.request({
            url: "entry/wxapp/Api",
            data: {
              m: "monai_market",
              r: "home.index.useredit_sbk",
              uid: uid,
              id: that.id,
              // 用户id
              title: name,
              // 店铺名
              tel: tel,
              // 联系电话
              content: cont,
              // 内容
              carimgs: g,
              // 图片（参考卖车页面的）
              address: addr
            },
            cachetime: "0",
            success: function (res) {
              console.log('修改成功：', res.data);
              wx.showModal({
                title: '提示',
                content: "修改成功",
                showCancel: false,
                success: function (a) {
                  wx.reLaunch({
                    url: "../mine/index"
                  });
                }
              });
            },
            fail: function (res) {
              console.log('修改失败：', res.data);
            }
          });
        }
      } else {
        that.setData({
          isShow: !0
        });
      }
    },
    // 上传图片
    upcarsimgs: function (a) {
      var e = this;
      9 <= e.carsimgs.length ? app.globalData.util.message({
        title: "您添加的图片已经很多了",
        type: "error"
      }) : app.globalData.upimgs({
        scannums: 9,
        filename: "cars",
        success: function (a) {
          for (var t in a) e.addinimgs(a[t]);
        }
      });
    },
    addinimgs: function (a) {
      var t = this.carsimgs,
          e = {
        img: a.all,
        imgshort: a.short
      };
      t.push(e), this.setData({
        carsimgs: t
      });
    },
    showimgs: function (a) {
      var t = a.currentTarget.dataset.index,
          e = this.carsimgs;
      wx.previewImage({
        current: e[t].img,
        urls: [e[t].img]
      });
    },
    upthiscarsimgs: function (a) {
      var t = a.currentTarget.dataset.index,
          e = this.carsimgs;
      app.globalData.util.request({
        url: "entry/wxapp/Api",
        data: {
          m: "monai_market",
          uid: app.globalData.getuid(),
          imgurl: e[t].img,
          r: "Upimg.delImg"
        }
      }), e[t].img = "", this.setData({
        carsimgs: e
      });
    },

    formReset() {
      console.log("占位：函数 formReset 未声明");
    },

    inpCont() {
      console.log("占位：函数 inpCont 未声明");
    }

  }
};
</script>
<style>
@import "./parts.css";

</style>